<template>
	<zero-custom-bar :effect="false" color="#fff" title='年检代办'
		backgroundColor="linear-gradient(180deg, #FE9B23 0%, #FEA71D 100%)" :singleIcon="true"></zero-custom-bar>

	<view class="car-number">
		<view class="upload-card" v-if="!state" @click="onAddCar">
			<image src="../../static/add-radio.png" mode=""></image>
			<text>添加车辆办年检</text>
		</view>
		<view class="top" v-else>
			<scroll-view scroll-x class="scroll">
				<view class="scroll-item" v-for="(item,index) in carList" :key="index" @click="handleKey(index)">
					<text class="key" :class="{active:activeKey==index}">{{item.carNumber}}</text>
					<text class="bar" :class="{'bar-active':activeKey==index}"></text>
				</view>
			</scroll-view>
			<text class="add-cat" @click="onAddCar">+新增车辆</text>
		</view>
		<view class="car-info" v-if="state">
			<view class="content">
				<text class="">年检到期预计还剩</text>
				<text class="date">{{carList[activeKey].days}}</text>
				<text>天</text>
			</view>
			<text class="time">注册日期：{{carList[activeKey].registrationDate}}</text>
			<image src="../../static/edit.png" @click="onEditCar" mode=""></image>
		</view>
		<text class="number">办理条件：</text>
		<view class="list">
			<view class="item">
				<image src="../../static/radio-g.png" mode=""></image>
				<text>需在法定年检有效期内(见行驶证副页,最多提前2个月)。</text>
			</view>
			<view class="item">
				<image src="../../static/radio-g.png" mode=""></image>
				<text>若有违章需全部处理。</text>
			</view>
			<view class="item">
				<image src="../../static/radio-g.png" mode=""></image>
				<text>车辆无改装,包括外观、灯光、轮毂、尾翼、牌照框等。</text>
			</view>
			<view class="item">
				<image src="../../static/radio-g.png" mode=""></image>
				<text>逾期车辆只能选择自驾到站的方式办理。</text>
			</view>




		</view>
	</view>
	<view class="info-box">
		<image :src="data.imgUrl" class="pic" mode="widthFix"></image>
	</view>
	<view class="footer">
		<view class="content">
			<view class="price">
				<text class="amount">{{data.salePrice/100}}元</text>
				<text class="unamount">原价({{data.initPrice/100}}元)</text>
			</view>
			<button class="btn" @click="submit">立即购买</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onReachBottom,
		onShow,

	} from '@dcloudio/uni-app'
	import {
		ref,
		reactive
	} from 'vue';
	import {
		getProductInfo,
		carInfoList
	} from '@/api'
	import {
		wxOpenid
	} from '@/utils/login-sdk.js'
	const data = ref({
		imgUrl: '',
		initPrice: '',
		salePrice: ''
	})
	/** 判断是否有车辆 */
	const state = ref(true)
	/** 当前选中的车牌 */
	const activeKey = ref(0)
	const carList = ref([])
	onLoad(async () => {
		getData()
		
	})
	onShow(()=>{
		getCarList()
	})
	/** 获取信息 */
	const getData = async () => {
		const res = await getProductInfo({
			businessType: 1
		})
		data.value = res.result

	}
	/** 获取车辆 */
	const getCarList = async () => {
		const {
			memberId
		} = await wxOpenid()
		const res = await carInfoList({
			memberId
		})
		carList.value = res.result
		
		if (carList.value.length) {
			state.value = true
		} else {
			state.value = false
		}
		
	}
	/** 切换车牌 */
	const handleKey = (index) => {
		activeKey.value = index
	}
	/** 新增车辆 */
	const onAddCar = ()=>{
		uni.navigateTo({
			url:'/pages/addCar/addCar'
		})
	}
	/** 修改车辆 */
	const onEditCar = ()=>{
		uni.navigateTo({
			url:'/pages/addCar/addCar?carId='+carList.value[activeKey.value].carInfoId
		})
	}
	/** 提交 */
	const submit = () => {
		if(carList.value.length){
			const carInfoId = carList.value[activeKey.value].carInfoId
			uni.navigateTo({
				url: "/pages/inspectionSubmit/inspectionSubmit?carInfoId="+carInfoId+'&productInfoId='+data.value.id+'&orderAmount='+data.value.salePrice 
			})
		}else{
			uni.showToast({
				title:'请添加车辆',
				icon:'none'
			})
		}
		
	}
</script>

<style lang="scss">
	page {
		background: linear-gradient(180deg, #FE9B23 0%, #FFB32B, #F7F4F0 49%, ) no-repeat;
		padding-bottom: constant(safe-area-inset-bottom);

		padding-bottom: env(safe-area-inset-bottom);
		/*兼容 IOS>11.2*/
		background-attachment: fixed
	}

	.car-number {
		width: 702rpx;
		padding-bottom: 30rpx;
		background: #FFFFFF;
		border-radius: 28rpx;
		margin: 45rpx auto 0;
		overflow: hidden;

		.upload-card {
			width: 640rpx;
			height: 201rpx;
			background: rgba(255, 221, 179, 0.28);
			border-radius: 28rpx;
			margin: 34rpx auto 42rpx;
			overflow: hidden;

			image {
				display: block;
				width: 48rpx;
				height: 48rpx;
				margin: 55rpx auto 14rpx;
			}

			text {
				display: block;
				text-align: center;
				font-size: 30rpx;
				color: #FE9C24;
			}

		}

		.top {
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			width: 640rpx;
			margin: 40rpx auto 0;

			.scroll {
				white-space: nowrap;
				width: 500rpx;

				.scroll-item {
					display: inline-block;
					margin-right: 42rpx;

					.key {
						font-weight: bold;
						font-size: 30rpx;
						color: #A8A8A8;
					}

					.active {
						color: #2B2B2B;
					}

					.bar {
						// display: none;
						width: 0;
						height: 9rpx;
						background: #FE9C24;
						border-radius: 5rpx;
						margin: 10rpx auto;
						transition: all .5s ease-in-out;
					}

					.bar-active {
						display: block;
						width: 83rpx;
					}
				}

				.scroll-item:last-of-type {
					margin-right: 0;
				}
			}

			.add-cat {
				font-size: 26rpx;
				color: #2B2B2B;
				display: block;
				line-height: 26rpx;
				height: 26rpx;
				margin-top: 8rpx;
			}
		}

		.car-info {
			width: 640rpx;
			height: 163rpx;
			background: #F6F6F6;
			border-radius: 28rpx;
			margin: 22rpx auto 43rpx;
			position: relative;
			overflow: hidden;

			.content {
				display: inline-block;
				margin: 20rpx 0 0 36rpx;
				line-height: 0;

				// vertical-align:bottom;
				// align-items: flex-end;
				text {
					// display: block;
					font-weight: bold;
					font-size: 30rpx;
					color: #2B2B2B;
					vertical-align: bottom;
					line-height: 1;
					/* 根据实际字体大小调整行高 */
					// line-height: 45rpx;
				}

				.date {
					font-size: 45rpx;
					color: #FF4800;
					margin: 6rpx 14rpx 0;
					// display: block;

				}
			}

			.time {
				display: block;
				font-size: 23rpx;
				color: #7C7C7C;
				margin: 25rpx 0 0 36rpx;
			}

			image {
				display: block;
				width: 39rpx;
				height: 39rpx;
				position: absolute;
				top: 27rpx;
				right: 41rpx;
			}
		}

		.number {
			font-weight: bold;
			font-size: 30rpx;
			color: #2b2b2b;
			display: block;
			margin-left: 32rpx;
			margin-bottom: 28rpx;
		}

		.list {
			.item {
				margin-bottom: 20rpx;
				display: flex;
				align-items: center;
				margin-left: 30rpx;

				image {
					width: 18rpx;
					height: 18rpx;
					display: block;
					margin-right: 11rpx;
				}

				text {

					font-size: 23rpx;
					color: #2B2B2B;
				}
			}
		}
	}

	.info-box {
		margin: 22rpx auto;
		width: 702rpx;
		border-radius: 28rpx;
		background: #fff;
	}

	.pic {
		display: block;
		margin: 22rpx auto;
		width: 702rpx;

		border-radius: 28rpx;
	}

	.footer {
		width: 748rpx;
		height: 166rpx;

		.content {
			background: #FFFFFF;
			position: fixed;
			left: 0;
			bottom: 0;
			width: 750rpx;
			height: 166rpx;
			display: flex;
			justify-content: space-between;

			.price {
				margin-left: 30rpx;
				margin-top: 30rpx;

				.amount {
					color: #FE9C26;
					font-weight: bold;
					display: block;
					font-size: 34rpx;
				}

				.unamount {
					color: #A8A8A8;
					font-size: 28rpx;
					display: block;
					text-decoration: line-through;
				}
			}

			.btn {
				width: 210rpx;
				height: 70rpx;
				background: #FE9C26;
				border-radius: 35rpx;
				font-size: 28rpx;
				color: #FFFBF6;
				line-height: 70rpx;
				margin-top: 30rpx;
				margin-right: 30rpx;
			}
		}


	}
</style>